using Verse;
using Verse.AI;

namespace RimWorld;

public class ThinkNode_ConditionalShouldFollowMaster : ThinkNode_Conditional
{
	protected override bool Satisfied(Pawn pawn)
	{
		return ShouldFollowMaster(pawn);
	}

	public static bool ShouldFollowMaster(Pawn pawn)
	{
		if (!pawn.Spawned || pawn.playerSettings == null)
		{
			return false;
		}
		Pawn respectedMaster = pawn.playerSettings.RespectedMaster;
		if (respectedMaster == null)
		{
			return false;
		}
		if (respectedMaster.Spawned)
		{
			if (pawn.playerSettings.followDrafted && respectedMaster.Drafted && pawn.CanReach(respectedMaster, PathEndMode.OnCell, Danger.Deadly))
			{
				return true;
			}
			if (pawn.playerSettings.followFieldwork && respectedMaster.mindState.lastJobTag == JobTag.Fieldwork && pawn.CanReach(respectedMaster, PathEndMode.OnCell, Danger.Deadly))
			{
				return true;
			}
		}
		else
		{
			Pawn carriedBy = respectedMaster.CarriedBy;
			if (carriedBy != null && carriedBy.HostileTo(respectedMaster) && pawn.CanReach(carriedBy, PathEndMode.OnCell, Danger.Deadly))
			{
				return true;
			}
		}
		return false;
	}
}
